package com.zte.login.controller;

import com.zte.login.pojo.UserModule;
import com.zte.login.pojo.Users;
import com.zte.login.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.TreeMap;


/**
 * @author luode
 * @Auther: 许月
 * @Date: 2022/10/01/17:21
 */
@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录功能
     * @param session 保存数据
     * @return 跳转页面
     */
    @RequestMapping("/login")
    public String login(Users users, HttpSession session){
        Users user = userService.login(users);
        if(user != null){
            System.out.println(user);
            session.setAttribute("user",user);
            return "main";
        }else{
            return "login";
        }
    }


    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    /**
     * 退出登录
     * @param session HttpSession
     * @return login
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }

    /**
     * 查询角色所拥有的权限
     * @param session HttpSession
     */
    @RequestMapping("/getModule")
    @ResponseBody
    public TreeMap<String, ArrayList<UserModule>> getModule(HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        return userService.getModule(user);
    }

}
